﻿using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm_Mes.ProductManagement.Product
{
    //添加规格窗体
    public partial class AddSpecificationForm : DevExpress.XtraEditors.XtraForm
    {
        private string baseUrl = "http://localhost:5164"; // 型号Api接口
        private HttpClient httpClient = new HttpClient();
        private List<TypeModel> typeList = new List<TypeModel>();
        public AddSpecificationForm()
        {
            InitializeComponent();
            this.Load += AddSpecificationForm_Load;
            //提交按钮点击事件
            this.SumitButton.Click += AddSpecificationButton;
        }
        // 1. 加载下拉
        private async void AddSpecificationForm_Load(object sender, EventArgs e)
        {
            await BindTypeComboBox();
        }
        private async Task BindTypeComboBox()
        {
            try
            {
                HttpResponseMessage response = await httpClient.GetAsync(baseUrl + "/api/Type/GetAllType");
                if (response.IsSuccessStatusCode)
                {
                    string json = await response.Content.ReadAsStringAsync();
                    var apiResponse = JsonConvert.DeserializeObject<ApiResponse<TypeModel>>(json);
                    typeList = apiResponse.items ?? new List<TypeModel>();

                    comboBoxTypeId.Properties.Items.Clear();
                    foreach (var item in typeList)
                    {
                        comboBoxTypeId.Properties.Items.Add(item.TypeName);
                    }
                    if (typeList.Count > 0)
                        comboBoxTypeId.SelectedIndex = 0;
                }
                else
                {
                    XtraMessageBox.Show("获取规格型号失败！");
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("发生错误：" + ex.Message);
            }
        }
        //添加规格信息方法
        private async void AddSpecificationButton(object sender, EventArgs e)
        {
            string selectedTypeName = comboBoxTypeId.SelectedItem?.ToString();
            var selectedType = typeList.FirstOrDefault(x => x.TypeName == selectedTypeName);
            if (selectedType == null)
            {
                XtraMessageBox.Show("请选择规格型号！");
                return;
            }
            long TypeId = selectedType.Id;
            string SpecificationCode = textAddSpecificationCode.Text.Trim();
            string SpecificationName = textAddSpecificationName.Text.Trim();
            if (string.IsNullOrEmpty(SpecificationCode) || string.IsNullOrEmpty(SpecificationName))
            {
                XtraMessageBox.Show("规格编号和规格名称不能为空！");
                return;
            }
            var specData = new
            {
                TypeId = TypeId, // 外键
                SpecificationCode = SpecificationCode,
                SpecificationName = SpecificationName
            };
            try
            {
                var content = new StringContent(JsonConvert.SerializeObject(specData), Encoding.UTF8, "application/json");
                var response = await httpClient.PostAsync(baseUrl + "/api/Specification/AddSpecification", content);

                if (response.IsSuccessStatusCode)
                {
                    XtraMessageBox.Show("添加成功！");
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    string error = await response.Content.ReadAsStringAsync();
                    XtraMessageBox.Show("添加失败：" + error);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("添加异常：" + ex.Message);
            }
        }
        //添加规格信息参数
        public class SpecificationModels
        {
            /// <summary>
            /// 规格型号
            /// </summary>
            public long TypeId { get; set; }
            /// <summary>
            /// 规格编码
            /// </summary>
            public string SpecificationCode { get; set; }
            /// <summary>
            /// 规格名称
            /// </summary>
            public string SpecificationName { get; set; }
        }
        // 下拉绑定规格型号实体
        public class TypeModel
        {
            // <summary>
            /// 规格型号Id
            /// </summary>
            public long Id { get; set; }
            /// <summary>
            /// 规格型号名称
            /// </summary>
            public string TypeName { get; set; }
        }
        // API返回包装
        public class ApiResponse<T>
        {
            public List<T> items { get; set; }
        }

        private void AddSpecificationForm_Load_1(object sender, EventArgs e)
        {

        }
    }
}